package org.duanyu.shopping_mail_service.service;

import com.sun.mail.util.MailSSLSocketFactory;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.apache.dubbo.config.annotation.DubboService;
import org.duanyu.shopping_common.result.BaseResult;
import org.duanyu.shopping_common.result.BusException;
import org.duanyu.shopping_common.result.CodeEnum;
import org.duanyu.shopping_common.service.MailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Properties;

@DubboService
@Service
public class MailServiceImpl implements MailService {

    @Value("${mail.user}")
    private String USER;  //发件人邮箱地址
    @Value("${mail.password}")
    private String PASSWORD; //如果是qq邮箱可以使用客户端授权码

    @Override
    public BaseResult sendMail(String to, String text, String title) {
        try {
            MailSSLSocketFactory factory=new MailSSLSocketFactory();
            factory.setTrustAllHosts(true);
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.socketFactory", factory);
            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 直接使用注入的用户名和密码用户名、密码
                    return new PasswordAuthentication(USER, PASSWORD);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            InternetAddress form=new InternetAddress(USER);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");

            // 开启SSL加密
            MailSSLSocketFactory sslSocketFactory = new MailSSLSocketFactory();
            sslSocketFactory.setTrustAllHosts(true);
            props.put("mail.smtp.ssl.enable",true);
            props.put("mail.smtp.ssl.socketFactory",sslSocketFactory);

            // 发送邮件
            Transport.send(message);
            return BaseResult.success();
        }catch (Exception e){
            e.printStackTrace();
            throw new BusException(CodeEnum.EMIL_SEND_ERROR);
        }
    }
}
